//
//  AppDelegate.swift
//  grid-ios-app
//
//  Created by wondertek on 2024/3/13.
//

import UIKit
import ESTabBarController_swift
import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // 设置语言
        setGridLanguage()
        UIAppearanceConfig()
        //键盘
        setupGridIQKeyboar()
        
        // 设置 window
        window = UIWindow.init(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        setRootViewController()
        return true
    }
    
    // 键盘相关 IQKeyboardManager
    private func setupGridIQKeyboar() {
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = true
        IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
    }
    
    private func UIAppearanceConfig() {
        
        UINavigationBar.appearance().tintColor = Theme.Color.NavigationBar.tint
        UINavigationBar.appearance().barTintColor = .white
        UINavigationBar.appearance().isTranslucent = false
        UINavigationBar.appearance().barStyle = .default
        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().titleTextAttributes = [
            .font: Theme.Font.size(14, bold: true),
            .foregroundColor: Theme.Color.NavigationBar.tint
        ]
        
        // 解决iOS15 barTintColor设置无效的问题，参考https://developer.apple.com/forums/thread/682420
        if #available(iOS 15.0, *) {
            let newAppearance = UINavigationBarAppearance()
            newAppearance.configureWithOpaqueBackground()
            newAppearance.backgroundColor = .white
            newAppearance.shadowImage = UIImage()
            newAppearance.shadowColor = nil
            newAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: Theme.Color.NavigationBar.tint, NSAttributedString.Key.font: Theme.Font.size(14, bold: true)]
            
            UINavigationBar.appearance().standardAppearance = newAppearance
            UINavigationBar.appearance().scrollEdgeAppearance =  UINavigationBar.appearance().standardAppearance
        }
        UIBarButtonItem
            .appearance(whenContainedInInstancesOf: [UINavigationBar.self])
            .setTitleTextAttributes([.font: Theme.Font.size(14, bold: false),
                                     .foregroundColor: Theme.Color.NavigationBar.tint], for: .normal)
        UIBarButtonItem
            .appearance(whenContainedInInstancesOf: [UINavigationBar.self])
            .setTitleTextAttributes([.font: Theme.Font.size(14, bold: false),
                                     .foregroundColor: Theme.Color.NavigationBar.tint], for: .highlighted)
        
        UIButton.appearance().isExclusiveTouch = true
        
        if #available(iOS 10, *) {
            UICollectionView.appearance().isPrefetchingEnabled = false
        }
        UIScrollView.appearance().delaysContentTouches = false
        
    }
    
}

extension AppDelegate {
    
    private func setGridLanguage() {
        //替换Bundle.main为自定义的MyBundle
        object_setClass(Foundation.Bundle.main, GridBundle.self)
        let lan = GridLanguageManager.currentLanguage()
        GridLanguageManager.shared.language = lan!
    }
    
    // 加上判断登录逻辑
    private func setRootViewController() {
        if GridLoginManager.shared.isLogin() {
            window?.rootViewController = gridTabBarControllerStyle()
        } else {
            window?.rootViewController = GridLoginViewController()
        }
        window?.makeKeyAndVisible()
    }
    
    private func gridTabBarControllerStyle() -> GridTabBarController {
        let tabBarController = GridTabBarController()
        let v1 = GridHomeViewController()
        let v2 = GridMeViewController()
        let navigationController1 = RTRootNavigationController.init(rootViewController: v1)
        let navigationController2 = RTRootNavigationController.init(rootViewController: v2)
        
        v1.tabBarItem = ESTabBarItem.init(TabBarHighlightableContentView(), title: "首页", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(TabBarHighlightableContentView(), title: "我的", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))
        
        tabBarController.viewControllers = [navigationController1, navigationController2]
        
        return tabBarController
    }
    
    func resetRootViewController() {
        setRootViewController()
    }
}

